package org.spindle.dungeon.entities;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.spindle.dungeon.Constants;
import org.spindle.dungeon.EntityManager;
import org.spindle.dungeon.LevelProperties;

import java.util.Date;
import java.util.List;


public class Player extends GeneralEntity {

    int dx = 0;
    int dy = 0;

    private Integer weaponLifetime = -1;

    private GeneralEntity weapon = null;

    @Override
    public void initialize() {
        //up
        animations[0] = new Animation(getAnimationArray(16, 0, 3), 200);

        //down
        animations[1] = new Animation(getAnimationArray(16, 1, 3), 200);

        //right
        animations[2] = new Animation(getAnimationArray(16, 2, 3), 200);

        //left
        animations[3] = new Animation(getAnimationArray(16, 3, 3), 200);

        hitBoxWidth = 10;
        hitBoxHeight = Constants.TILE_SIZE;
        offsetHitboxX = 3;
    }

    @Override
    public void update(GameContainer gc, LevelProperties props) {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_UP)) {
            direction = 0;
            if (canMove(0, -1)) {
                y--;
            }
            dx = 0;
            dy = -1;
        }

        if (input.isKeyDown(Input.KEY_DOWN)) {
            direction = 1;
            if (canMove(0, 1)) {
                y++;
            }
            dx = 0;
            dy = 1;
        }

        if (input.isKeyDown(Input.KEY_LEFT)) {
            direction = 3;
            if (canMove(-1, 0)) {
                x--;
            }
            dx = -1;
            dy = 0;
        }

        if (input.isKeyDown(Input.KEY_RIGHT)) {
            direction = 2;
            if (canMove(1, 0)) {
                x++;
            }
            dx = 1;
            dy = 0;
        }

        if (input.isKeyDown(Input.KEY_1)) {
            weapon = new Sword();
            weaponLifetime = 0;
        }

        if (input.isKeyPressed(Input.KEY_SPACE)) {

            if (weapon != null) {
                weapon.reset();
                weapon.x = x + dx * 16;
                weapon.y = y + dy * 16;
                weapon.direction = direction;
                weaponLifetime = weapon.lifeTime + 2;
                EntityManager.getInstance().registerEntity(weapon);
            } else {
                System.out.println("There is no weapon selected");
            }

        }

        if (input.isKeyPressed(Input.KEY_Z)) {
            List<GeneralEntity> ent = EntityManager.getInstance().returnEntitiesAt(this, dx * 2, dy * 2);
            if (ent != null) {
                for (GeneralEntity entity : ent) {
                    if (Constants.debug)
                        System.out.println(entity);
                    if (entity.triggerable) {
                        entity.trigger(this);
                    }
                }
            }
        }

        if (x > Constants.STAGE_WIDTH / 2) {
            props.cameraX = x - Constants.STAGE_WIDTH / 2;
        }
        if (y > Constants.STAGE_HEIGHT / 2) {
            props.cameraY = y - Constants.STAGE_HEIGHT / 2;
        }
    }


}
